package it.xiuxian.login.presenter


import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.HttpHeaders
import com.lzy.okgo.model.Response
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseBean

import it.xiuxian.lib.base.BasePresenter
import it.xiuxian.lib.base.ILoadBind
import it.xiuxian.lib.http.CallBackOption
import it.xiuxian.lib.http.HttpConfig
import it.xiuxian.lib.http.SPConfig
import it.xiuxian.lib.utils.*
import it.xiuxian.login.activity.LoginActivity
import it.xiuxian.login.bean.LoginBean
import it.xiuxian.login.bean.LoginBean1
import org.json.JSONObject


class LoginActivityPresenter:BasePresenter<LoginActivity>() {
    var heads:HttpHeaders?=null
    fun login(phone:String,password:String){
        var json1 = JSONObject()
        json1.put("loginname",phone)
            .put("password",password)
        OkGo.post<BaseBean<LoginBean>>(HttpConfig.BASE_URL+"/applogin/login")
            .params("encrypt", EncodeUtils.encodeAES(json1.toString()))
            .execute(object : CallBackOption<BaseBean<LoginBean>>() {}.loadBind(mView as LoginActivity).execute(
                object : ILoadBind<BaseBean<LoginBean>> {
                    override fun excute(bean: BaseBean<LoginBean>) {
                        if(bean.code==0){
                            ToastUtil.showShort(bean.msg)
                            if(OkGo.getInstance().commonHeaders!=null){
                                heads = OkGo.getInstance().commonHeaders
                            }else{
                                 heads = OkGo.getInstance().addCommonHeaders(OkGo.getInstance().commonHeaders).commonHeaders
                            }
                            heads!!.put(SPConfig.SESSION_ID,bean.data.token)
                            SPUtil.put(SPConfig.SESSION_ID,bean.data.token)
                            SPUtil.put(SPConfig.ISFIRST,"1")
                            ARouter.getInstance().build(ArouterAddress.MAINACTIVITY).withString("sessionId",bean.data.token).navigation()
                        }
                    }
                }
            ))

    }

}



